package com.schedule.service.impl;

import com.github.pagehelper.PageHelper;
import com.schedule.Util.PagedGridResult;
import com.schedule.mapper.FileMapper;
import com.schedule.pojo.CompetitionGradeInfo;
import com.schedule.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.commons.io.FilenameUtils;

@Service
public class FileServiceImpl implements IFileService {

    @Autowired
    private FileMapper fileMapper;


    @Override
    public boolean addFile(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String name = FilenameUtils.getBaseName(fileName); //获得文件的前缀
        String extension = FilenameUtils.getExtension(file.getOriginalFilename()); //获得文件的后缀
        String newFileNamePrefix= name + new SimpleDateFormat("yyyyMMddHHssSSS").format(new Date());
//        String newFileNamePrefix = name;
        String newFileName=newFileNamePrefix+"."+extension;
        String c = System.getProperty("user.dir"); //获得本电脑的相对路径
        String filePath = c + "/src/main/resources/uploadFiles/";
//        String filePath = "/src/main/resources/uploadFiles/";
        File dest = new File(filePath + newFileName);
        try {
            file.transferTo(dest);
            fileMapper.addFile(newFileName,filePath);
            return true;
        } catch (IOException e) {

        }
        return false;
    }

    /**
     * 模糊查询文件
     *
     * @param name
     */
    @Override
    public List<com.schedule.pojo.File> findFileByDim(String name) {
        return fileMapper.findFileByDim(name);
    }


    /**
     * 分页查询所有文件
     * page: 第几页
     * pageSize: 每页显示条数
     */
    public PagedGridResult findAll(Integer page, Integer pageSize){
        PageHelper.startPage(page, pageSize);
        List<com.schedule.pojo.File> players = fileMapper.findAll();
        PagedGridResult gridResult = PagedGridResult.setterPagedGrid(players, page);
        return gridResult;
    }

    /**
     * 根据文件名删除文件
     *
     * @param filename
     */
    @Override
    public boolean delFile(String filename) {
        String c = System.getProperty("user.dir"); //获得本电脑的相对路径
        String filePath = c + "\\src\\main\\resources\\uploadFiles\\"+filename;
        File file = new File(filePath);
        if (file.exists()){
            file.delete();
            int i = fileMapper.delFile(filename);
            if (i!=0){
                return true;
            }
            return false;
        }else {
            return false;
        }
    }
}
